from rest_framework import serializers
from rest_framework.response import Response
from .models import Grade,Course,Student,Teacher
class GradeSerializer(serializers.ModelSerializer):
    class Meta:
        #serializer model
        model = Grade
        # serializer fields
        fields = ['name']
        

class CourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = ('name',)

class BaseSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        data = super().to_representation(instance)
        if data['gender'] is True:
            data['gender'] = '男'
        else: 
            data['gender'] = '女'
        return data   
           
    
class StudentSerializer(serializers.ModelSerializer):

    c = serializers.SlugRelatedField(slug_field='name',many=True,queryset=Course.objects.all())
    g = serializers.SlugRelatedField(slug_field='name',queryset = Grade.objects.all())
    
 # 抽离出来 到继承 BASE  class--BaseSerializer 可以简写&不写
    def to_representation(self, instance):
        data = super().to_representation(instance)
        if data['gender'] is True:
            data['gender'] = '男'
        else: 
            data['gender'] = '女'
        return data
        
    class Meta:
        g = serializers.SlugRelatedField(slug_field='name',queryset = Grade.objects.all())
        c  = serializers.SlugRelatedField(slug_field='name',queryset = Course.objects.all(),many=True)
    
        model = Student
        extra_kwargs = {'age':{'max_value':30,'min_value':0}}
        fields = '__all__'
        
            
class TeacherSerializer(BaseSerializer):
    c = serializers.SlugRelatedField(slug_field='name',many=True,queryset=Course.objects.all())
    g = serializers.SlugRelatedField(slug_field='name',many=True,queryset=Grade.objects.all())
    class Meta:
      
        model = Teacher
        fields = '__all__'

    
        
            
    